package com.gyh.thymeleaf.controller;

import com.gyh.thymeleaf.codeutile.IVerifyCodeGen;
import com.gyh.thymeleaf.codeutile.SimpleCharVerifyCodeGenImpl;
import com.gyh.thymeleaf.codeutile.VerifyCode;
import com.gyh.thymeleaf.entity.User;
import com.gyh.thymeleaf.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.List;

/**
 * @author gyhstart
 * @create 2020/6/25 - 17:38
 **/

@Api(tags = "登录测试模块")
@Controller
@RequestMapping("/api")
public class LoginController {
    private static final Logger logger = LoggerFactory.getLogger(SwaggerController.class);

    //验证码
    private String  randomCode;

    @Autowired
    private UserService userService;

    @GetMapping("/login")
    @ApiOperation(value = "登录界面", notes = "无需参数", httpMethod = "GET")
    public String login() {
        return "login";
    }

    @GetMapping("/verifyCode")
    @ApiOperation(value = "验证码")
    public void verifyCode(HttpServletRequest request, HttpServletResponse response) {
        IVerifyCodeGen iVerifyCodeGen = new SimpleCharVerifyCodeGenImpl();
        try {
            //设置长宽
            VerifyCode verifyCode = iVerifyCodeGen.generate(80, 28);
            String code = verifyCode.getCode();
            logger.info(code);
            randomCode = code;
            //将VerifyCode绑定session
            request.getSession().setAttribute("VerifyCode", code);
            //设置响应头
            response.setHeader("Pragma", "no-cache");
            //设置响应头
            response.setHeader("Cache-Control", "no-cache");
            //在代理服务器端防止缓冲
            response.setDateHeader("Expires", 0);
            //设置响应内容类型
            response.setContentType("image/jpeg");
            response.getOutputStream().write(verifyCode.getImgBytes());
            response.getOutputStream().flush();
        } catch (IOException e) {
            logger.info("", e);
        }
    }

    @GetMapping("/tologin")
    @ApiOperation(value = "登录验证", notes = "登录需要用户名、密码等需参数", httpMethod = "GET")
    public String tologin(User user, String captcha, HttpSession session) {
        List<User> userList = userService.login(user);
        if(null == userList || userList.size() == 0 || !randomCode.equals(captcha)){
            logger.info("登陆失败");
            return "login";
        }
        logger.info("登陆成功");
        session.setAttribute("userInfo",user.getName());
        return "/dashboard/home";
    }

    @GetMapping("/dashboard")
    @ApiOperation(value = "主页", notes = "无需参数", httpMethod = "GET")
    public String dashboard() {
        logger.info("主页");
        return "/dashboard/home";
    }

    @GetMapping("/logout")
    @ApiOperation(value = "退出登录", notes = "无需参数", httpMethod = "GET")
    public String logout() {
        logger.info("退出登录");
        return "/login";
    }
}
